package ch.nostromo.tiffanys.clients.desktop.gui.engine;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;

import uk.ac.cam.cl.projectcharlie2010.clients.desktop.controller.DesktopController;
import uk.ac.cam.cl.projectcharlie2010.game.GameControllerResult;

import ch.nostromo.lib.swing.NosButton;
import ch.nostromo.lib.swing.NosInternalFrame;
import ch.nostromo.lib.swing.NosPanel;
import ch.nostromo.tiffanys.game.move.MoveTools;

public class EngineFrame extends NosInternalFrame {

  private static final long serialVersionUID = 1L;

  private JTextArea engineStats;
  private NosPanel panelTop;
  private NosButton btnMoveNow;

  private JScrollPane scrollPane;

  public EngineFrame() {
    super();
    initGUI();
  }

  private void initGUI() {

      BorderLayout thisLayout = new BorderLayout();
      this.getContentPane().setLayout(thisLayout);
      setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
      this.setPreferredSize(new java.awt.Dimension(302, 392));
      this.setBounds(0, 0, 302, 385);
      {
        scrollPane = new JScrollPane();
        scrollPane.setPreferredSize(new java.awt.Dimension(300, 360));
        this.getContentPane().add(scrollPane, BorderLayout.CENTER);
        {
          engineStats = new JTextArea();
          scrollPane.setViewportView(engineStats);
        }
      }
      {
        panelTop = new NosPanel();
        BorderLayout panelTopLayout = new BorderLayout();
        getContentPane().add(panelTop, BorderLayout.NORTH);
        panelTop.setPreferredSize(new java.awt.Dimension(300, 46));
        panelTop.setLayout(panelTopLayout);
        {
          btnMoveNow = new NosButton();
          panelTop.add(btnMoveNow, BorderLayout.CENTER);
          btnMoveNow.setText("Abort Engine");
          btnMoveNow.setBounds(12, 12, 126, 22);
          btnMoveNow.setTextTranslationKey("Engine.MoveNow");
          btnMoveNow.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
             moveNowInvoked();
            }
          });
        }
      }
      this.setTextTranslationKey("Engine.Title");
      pack();

  }

  public void setEngineStats(GameControllerResult gameControllerResult) {

      String stats = gameControllerResult.getEngineStatsFull();

      String moves = "";
      if (!gameControllerResult.isOpeningMove()) {
        moves =  MoveTools.getMovesDump(gameControllerResult.getCalculatedMoves());
      }
      engineStats.setText(stats  + moves);
  }

  public void setEngineMessage(String msg) {
    engineStats.setText(msg);
  }

  void moveNowInvoked() {
    DesktopController.getInstance().getDesktop().engineMoveNow();
  }
  
  public void setComputing(boolean calculating) {
    this.btnMoveNow.setEnabled(calculating);
  }

}
